<?php

class WatermarkComponent extends Object {
 	
    var $edgePadding=15;                        // used when placing the watermark near an edge
    var $quality=90;                           // used when generating the final image
    var $watermark='img/watermark.png';  // the default image to use if no watermark was chosen
    
    
    public function watermark($file, $target = null){   
        
    		if($target == null){
    			$target = $file;
    		}	
    	
            // file upload success
            $size=getimagesize($file);
            if($size[2]==2 || $size[2]==3){
                // it was a JPEG or PNG image, so we're OK so far
                
                $original=$file;
                $wmTarget=$this->watermark.'.tmp';

                $origInfo = getimagesize($original); 
                $origWidth = $origInfo[0]; 
                $origHeight = $origInfo[1]; 

                $waterMarkInfo = getimagesize($this->watermark);
                $waterMarkWidth = $waterMarkInfo[0];
                $waterMarkHeight = $waterMarkInfo[1];
        
                // watermark sizing info
                $waterMarkDestWidth=$origWidth;
                $waterMarkDestHeight=$origHeight;
                $waterMarkDestWidth-=2*$this->edgePadding;
                $waterMarkDestHeight-=2*$this->edgePadding;
               

                // OK, we have what size we want the watermark to be, time to scale the watermark image
                $this->resize_png_image($this->watermark,$waterMarkDestWidth,$waterMarkDestHeight,$wmTarget);
                
                // get the size info for this watermark.
                $wmInfo=getimagesize($wmTarget);
                $waterMarkDestWidth=$wmInfo[0];
                $waterMarkDestHeight=$wmInfo[1];

                $differenceX = $origWidth - $waterMarkDestWidth;
                $differenceY = $origHeight - $waterMarkDestHeight;

                // where to place the watermark?
                
                        $placementX =  round($differenceX / 2);
                        $placementY =  round($differenceY / 2);
                       
       
                if($size[2]==3)
                    $resultImage = imagecreatefrompng($original);
                else
                    $resultImage = imagecreatefromjpeg($original);
                imagealphablending($resultImage, TRUE);
        
                $finalWaterMarkImage = imagecreatefrompng($wmTarget);
                $finalWaterMarkWidth = imagesx($finalWaterMarkImage);
                $finalWaterMarkHeight = imagesy($finalWaterMarkImage);
        
                imagecopy($resultImage,
                          $finalWaterMarkImage,
                          $placementX,
                          $placementY,
                          0,
                          0,
                          $finalWaterMarkWidth,
                          $finalWaterMarkHeight
                );
                
                if($size[2]==3){
                    imagealphablending($resultImage,FALSE);
                    imagesavealpha($resultImage,TRUE);
                    imagepng($resultImage,$target,$this->quality);
                }else{
                    imagejpeg($resultImage,$target,$this->quality); 
                }

                imagedestroy($resultImage);
                imagedestroy($finalWaterMarkImage);
			}
    }
    
	protected function resize_png_image($img,$newWidth,$newHeight,$target){
	    $srcImage=imagecreatefrompng($img);
	    if($srcImage==''){
	        return FALSE;
	    }
	    $srcWidth=imagesx($srcImage);
	    $srcHeight=imagesy($srcImage);
	    $percentage=(double)$newWidth/$srcWidth;
	    $destHeight=round($srcHeight*$percentage)+1;
	    $destWidth=round($srcWidth*$percentage)+1;
	    if($destHeight > $newHeight){
	        // if the width produces a height bigger than we want, calculate based on height
	        $percentage=(double)$newHeight/$srcHeight;
	        $destHeight=round($srcHeight*$percentage)+1;
	        $destWidth=round($srcWidth*$percentage)+1;
	    }
	    $destImage=imagecreatetruecolor($destWidth-1,$destHeight-1);
	    if(!imagealphablending($destImage,FALSE)){
	        return FALSE;
	    }
	    if(!imagesavealpha($destImage,TRUE)){
	        return FALSE;
	    }
	    if(!imagecopyresampled($destImage,$srcImage,0,0,0,0,$destWidth,$destHeight,$srcWidth,$srcHeight)){
	        return FALSE;
	    }
	    if(!imagepng($destImage,$target)){
	        return FALSE;
	    }
	    imagedestroy($destImage);
	    imagedestroy($srcImage);
	    return TRUE;
	}
    
}